home *** CD-ROM | disk | FTP | other *** search
/ World of Video / World of Video.iso / gfxprograms / 3dprograms / t3dlib / source / writeray.c < prev    next >
C/C++ Source or Header  |  1995-02-13  |  13KB  |  486 lines

  1. /* writeray.c - dump the internal database to a Rayshade 4.0 input file
  2.  *            - written by Glenn M. Lewis - 7/22/91
  3.  */
  4.  
  5. static char rcs_id[] = "$Id: writeray.c,v 1.14 1993/02/14 07:27:06 glewis Exp $";
  6.  
  7. #include <stdio.h>
  8. #include <ctype.h>
  9. #include "t3dlib.h"
  10. #ifdef __STDC__
  11. #include <stdlib.h>
  12. #include <strings.h>
  13. #include "writeray_protos.h"
  14. #endif
  15.  
  16. static void process_DESC();
  17. static void process_EXTR();
  18. static void process_INFO();
  19. static void process_OBJ();
  20.  
  21. /* Two-space tabs */
  22. #define TABSTOP "  "
  23.  
  24. static FILE *out;
  25. static char tab[133];
  26. static int num_OBJ, num_DESC, num_TOBJ;
  27. static int cur_level, cur_objnum;
  28. static int prntline;
  29.  
  30. static struct save_hier {
  31.     int objnum;
  32.     struct save_hier *next;
  33. } *root = 0;
  34. typedef struct save_hier HIER;
  35.  
  36. /* Here are a few necessary utilities */
  37.  
  38. static void indent()
  39. {
  40.     strcat(tab, TABSTOP);
  41. }
  42.  
  43. static void outdent()
  44. {
  45.     register int i = strlen(tab) - strlen(TABSTOP);
  46.     if (i<0) {
  47.         fprintf(stderr, "Whoa, Glenn!  You blew it!\n");
  48.         tab[0] = '\0';
  49.         return;
  50.     }
  51.     tab[i] = '\0';
  52. }
  53.  
  54. static void send_XYZ(f)            /* Print a common string */
  55. XYZ_st *f;
  56. {
  57.     fprintf(out, " %.12g",   f->x);
  58.     fprintf(out, " %.12g",   f->y);
  59.     fprintf(out, " %.12g\n", f->z);
  60. }
  61.  
  62. static void send_RGB(rgb)            /* Print a common string */
  63. RGB_st *rgb;
  64. {
  65.     fprintf(out, " %.12g",   ((double)rgb->r)/255.0);
  66.     fprintf(out, " %.12g",   ((double)rgb->g)/255.0);
  67.     fprintf(out, " %.12g\n", ((double)rgb->b)/255.0);
  68. }
  69.  
  70. /********************/
  71. /* The MAIN section */
  72. /********************/
  73.  
  74. int write_Rayshade(world, file)
  75. WORLD *world;
  76. FILE *file;
  77. {
  78.     register OBJECT *o;
  79.  
  80.     if (!(out=file) || !world) return(0);    /* File not open */
  81.  
  82.     tab[0] = '\0';
  83.     num_OBJ = num_DESC = num_TOBJ = 0;
  84.  
  85.     fputs("/* TTDDD library - Written by Glenn M. Lewis - 7/22/91*/\n\n", out);
  86.     if (world->info) process_INFO(world->info);
  87.     for (o=world->object; o; o=o->next)
  88.         process_OBJ(o);
  89.     return(1);
  90. }
  91.  
  92. static void process_INFO(info)
  93. INFO *info;
  94. {
  95.     fprintf(out, "%s/* INFO Begin */\n", tab);
  96.     indent();
  97. /*
  98.     for (i=0; i<8; i++)
  99.         if (info->brsh[i][0])
  100.             fprintf(out, "%sBRSH[%d]=\"%s\"\n", tab, i, info->brsh[i]);
  101.  
  102.     for (i=0; i<8; i++)
  103.         if (info->stnc[i][0])
  104.             fprintf(out, "%sSTNC[%d]=\"%s\"\n", tab, i, info->stnc[i]);
  105.  
  106.     for (i=0; i<8; i++)
  107.         if (info->txtr[i][0])
  108.             fprintf(out, "%sTXTR[%d]=\"%s\"\n", tab, i, info->txtr[i]);
  109. */
  110.  
  111.     if (info->obsv) {
  112.         fprintf(out, "%seyep", tab); send_XYZ(&info->obsv->came);
  113.         fprintf(out, "%s/* OBSV Rotate", tab); send_XYZ(&info->obsv->rota);
  114.         fprintf(out, "*/%sfocaldist  %.12g\n", tab, info->obsv->foca);
  115.     }
  116.  
  117. /*    if (info->otrk[0]) fprintf(out, "%sOTRK \"%s\"\n", tab, info->otrk);    */
  118.  
  119. /*
  120.     if (info->ostr) {
  121.         if (info->ostr->path[0])
  122.             fprintf(out, "%sOSTR Path \"%s\"\n", tab, info->ostr->path);
  123.         fprintf(out, "%sOSTR Translate", tab); send_XYZ(&info->ostr->tran);
  124.         fprintf(out, "%sOSTR Rotate   ", tab); send_XYZ(&info->ostr->rota);
  125.         fprintf(out, "%sOSTR Scale    ", tab); send_XYZ(&info->ostr->scal);
  126.         i = info->ostr->info;
  127.         strin[0] = '\0';
  128.         if (i&(1<<0))  strcat(strin, " ABS_TRA");
  129.         if (i&(1<<1))  strcat(strin, " ABS_ROT");
  130.         if (i&(1<<2))  strcat(strin, " ABS_SCL");
  131.         if (i&(1<<4))  strcat(strin, " LOC_TRA");
  132.         if (i&(1<<5))  strcat(strin, " LOC_ROT");
  133.         if (i&(1<<6))  strcat(strin, " LOC_SCL");
  134.         if (i&(1<<8))  strcat(strin, " X_ALIGN");
  135.         if (i&(1<<9))  strcat(strin, " Y_ALIGN");
  136.         if (i&(1<<10)) strcat(strin, " Z_ALIGN");
  137.         if (i&(1<<12)) strcat(strin, " FOLLOW_ME");
  138.         fprintf(out, "%sOSTR Info%s\n", tab, strin);
  139.     }
  140. */
  141.  
  142.     if (info->fade) {
  143.         fprintf(out, "%s/* FADE FadeAt %.12g*/\n", tab, info->fade->at);
  144.         fprintf(out, "%s/* FADE FadeBy %.12g*/\n", tab, info->fade->by);
  145.         fprintf(out, "%s/* FADE FadeTo", tab); send_RGB(&info->fade->to);
  146.         fprintf(out, "*/\n");
  147.     }
  148.  
  149.     if (info->skyc) {
  150.         fprintf(out, "%s/* SKYX Horizon", tab); send_RGB(&info->skyc->hori);
  151.         fprintf(out, "*/\n");
  152.         fprintf(out, "%s/* SKYC Zenith ", tab); send_RGB(&info->skyc->zeni);
  153.         fprintf(out, "*/\n");
  154.     }
  155.  
  156.     if (info->ambi)
  157.         { fprintf(out, "%sbackground", tab); send_RGB(info->ambi); }
  158.  
  159. /*
  160.     if (info->glb0)
  161.         for (i=0; i<8; i++)
  162.             fprintf(out, "%sGLB0[%d]=%u\n", tab, i, info->glb0[i]);
  163. */
  164.  
  165.     outdent();
  166.     fprintf(out, "%s/* End INFO */\n", tab);
  167. }
  168.  
  169. static void process_OBJ(obj)
  170. register OBJECT *obj;
  171. {
  172.     register HIER *p;
  173.     num_OBJ++;
  174.     fprintf(out, "%s/* OBJ Begin \"Hierarchy %d\" */\n", tab, num_OBJ);
  175.     num_DESC = num_TOBJ = 0;        /* Reset counters */
  176.     cur_level = 0;
  177.     cur_objnum = 1;
  178.     prntline = 1;
  179.  
  180.     if (obj->extr) process_EXTR(obj->extr);
  181.     else process_DESC(obj);
  182.  
  183.     while (root) {                /* This should happen at most once. */
  184.         p = root->next;
  185.         free((char *)root);        /* Delete this from list */
  186.         root = p;
  187.         outdent();
  188.     }
  189.  
  190.     fprintf(out, "%s/* End OBJ   \"Hierarchy %d\" */\n", tab, num_OBJ);
  191. }
  192.  
  193. static void process_TOBJ()
  194. {
  195.     register HIER *p;
  196.     if (num_DESC-num_TOBJ < cur_level) {    /* Pop old level off HIER */
  197.         cur_level--;
  198.         cur_objnum = root->objnum;
  199.         p = root->next;
  200.         free((char *)root);    /* Delete from list */
  201.         root = p;
  202.         outdent();    /* Pretty file formatting */
  203.     }
  204.     fprintf(out, "%s/* TOBJ       \"Object %d at level %d of hierarchy %d\" */\n",
  205.         tab, cur_objnum-1, num_DESC-num_TOBJ, num_OBJ);
  206.     num_TOBJ++;
  207.     prntline = 1;
  208. }
  209.  
  210. static void process_EXTR(extr)
  211. EXTR *extr;
  212. {
  213.     if (!prntline) fprintf(out, "\n");    /* Print one anyway */
  214.     indent();
  215.     num_DESC++;
  216.  
  217.     fprintf(out, "%s/* EXTR Begin \"Object %d at level %d of hierarchy %d\" */\n",
  218.         tab, cur_objnum, num_DESC-num_TOBJ, num_OBJ);
  219.     indent();
  220.     fprintf(out, "%s/* LOAD \"%s\" */\n", tab, extr->filename);
  221.     fprintf(out, "#include \"%s\"\n", extr->filename);
  222.     fprintf(out, "%stranslate", tab); send_XYZ(&extr->mtrx.tran);
  223.     fprintf(out, "%sscale    ", tab); send_XYZ(&extr->mtrx.scal);
  224.     fprintf(out, "%stransform", tab);
  225.     fprintf(out, " %.12g %.12g %.12g",
  226.         extr->mtrx.rota1.x,
  227.         extr->mtrx.rota1.y,
  228.         extr->mtrx.rota1.z);
  229.     fprintf(out, " %.12g %.12g %.12g",
  230.         extr->mtrx.rota2.x,
  231.         extr->mtrx.rota2.y,
  232.         extr->mtrx.rota2.z);
  233.     fprintf(out, " %.12g %.12g %.12g",
  234.         extr->mtrx.rota3.x,
  235.         extr->mtrx.rota3.y,
  236.         extr->mtrx.rota3.z);
  237.     fprintf(out, "\n");
  238.  
  239.     outdent();
  240.     fprintf(out, "%s/* End EXTR   \"Object %d at level %d of hierarchy %d\" */\n",
  241.         tab, cur_objnum, num_DESC-num_TOBJ, num_OBJ);
  242.  
  243.     num_TOBJ++;
  244.     cur_objnum++;
  245.     outdent();
  246.     prntline = 1;
  247. }
  248.  
  249. static void process_DESC(object)
  250. OBJECT *object;
  251. {
  252.     register int i;
  253.     register XYZ_st *p1, *p2, *p3;
  254.     register HIER *p;
  255.     register OBJECT *obj;
  256.     register DESC *desc = object->desc;
  257.  
  258.     num_DESC++;
  259.     if (num_DESC-num_TOBJ > cur_level) {    /* Push new level in HIER */
  260.         if (!prntline) fprintf(out, "\n");    /* Print one anyway */
  261.         if (!(p = (HIER*)malloc(sizeof(HIER)))) {
  262.             fprintf(stderr, "ERROR!  Out of memory.\n*** ABORT ***\n");
  263.             exit(20);
  264.         }
  265.         p->next = root;        /* Insert into list */
  266.         root = p;
  267.         root->objnum = cur_objnum;
  268.         cur_level++;
  269.         cur_objnum = 1;
  270.         indent();    /* Pretty file formatting */
  271.     }
  272.  
  273.     if (desc->shap && desc->shap[1]>0) {
  274.         fprintf(out, "%slight", tab);
  275.         if (desc->colr) send_RGB(desc->colr); else fprintf(out, " 1.0 ");
  276.         if (desc->shap[1]==1)    /* Sunlight */
  277.             fprintf(out, "directional %.12g %.12g %.12g\n",
  278.                 -(desc->posi->x),
  279.                 -(desc->posi->y),
  280.                 -(desc->posi->z));
  281.         else    /* Like a lamp */
  282.             fprintf(out, "point %.12g %.12g %.12g\n",
  283.                 desc->posi->x,
  284.                 desc->posi->y,
  285.                 desc->posi->z);
  286.         if (!desc->pcount && !desc->ecount && !object->child) goto SKIP_REST_LIGHT;
  287.     }
  288.  
  289.     fprintf(out,"%s/* DESC Begin \"Object %d at level %d of hierarchy %d\" */\n",
  290.         tab, cur_objnum, num_DESC-num_TOBJ, num_OBJ);
  291.     fprintf(out, "%sgrid 20 20 20\n", tab);
  292.     indent();
  293.  
  294.     if (desc->name[0]) fprintf(out, "%s/* name \"%s\" */\n", tab, desc->name);
  295.  
  296.     if (desc->shap) {
  297.         fprintf(out, "%s/* SHAP Shape = %u */\n", tab, desc->shap[0]);
  298.         if (desc->shap[0]==0 && !desc->shap[1]) {    /* Sphere */
  299.             fprintf(out, "%ssphere %.12g", tab, desc->size->x);
  300.             send_XYZ(desc->posi);
  301.             goto SKIP_REST;
  302.         }
  303.         fprintf(out, "%s/* SHAP Lamp  = %u */\n", tab, desc->shap[1]);
  304.     }
  305.  
  306. /*
  307.     if (desc->axis) {
  308.         fprintf(out, "%sAXIS XAxis", tab); send_XYZ(&desc->axis->xaxi);
  309.         fprintf(out, "%sAXIS YAxis", tab); send_XYZ(&desc->axis->yaxi);
  310.         fprintf(out, "%sAXIS ZAxis", tab); send_XYZ(&desc->axis->zaxi);
  311.     }
  312.  
  313.     if (desc->size)
  314.         { fprintf(out, "%sSIZE", tab); send_XYZ(desc->size); }
  315.  
  316.     if (desc->pcount) {
  317.         fprintf(out, "%sPNTS PCount %u\n", tab, desc->pcount);
  318.         for (i=0; i<desc->pcount; i++) {
  319.             fprintf(out, "%sPNTS Point[%d]", tab, i);
  320.             send_XYZ(&desc->pnts[i]);
  321.         }
  322.     }
  323.  
  324.     if (desc->ecount) {
  325.         fprintf(out, "%sEDGE ECount %u\n", tab, desc->ecount);
  326.         for (i=0; i<desc->ecount; i++) {
  327.             fprintf(out, "%sEDGE Edge[%d] %u %u\n", tab, i,
  328.                 desc->edge[i<<1], desc->edge[(i<<1)+1]);
  329.         }
  330.     }
  331. */
  332.  
  333.     if (desc->fcount && desc->ecount && desc->pcount) {
  334.         fprintf(out, "%s/* FACE TCount %u */\n", tab, desc->fcount);
  335.         for (i=0; i<desc->fcount; i++) {
  336. /*            fprintf(out, "%sFACE Connect[%u] %u %u %u\n", tab, i,
  337.                 desc->face[i*3], desc->face[i*3+1], desc->face[i*3+2]);
  338. */
  339.             /* First check and see if the triangle is degenerate */
  340.             p1 = &desc->pnts[desc->edge[(desc->face[i*3])<<1]];
  341.             p2 = &desc->pnts[desc->edge[((desc->face[i*3])<<1)+1]];
  342.             if (desc->edge[(desc->face[i*3+2])<<1] ==
  343.                 desc->edge[(desc->face[i*3])<<1] ||
  344.                 desc->edge[(desc->face[i*3+2])<<1] ==
  345.                 desc->edge[((desc->face[i*3])<<1)+1])
  346.                 p3 = &desc->pnts[desc->edge[((desc->face[i*3+2])<<1)+1]];
  347.             else
  348.                 p3 = &desc->pnts[desc->edge[(desc->face[i*3+2])<<1]];
  349.             if (!((p1->x==p2->x && p1->y==p2->y && p1->z==p2->z) ||
  350.                   (p1->x==p3->x && p1->y==p3->y && p1->z==p3->z) ||
  351.                   (p2->x==p3->x && p2->y==p3->y && p2->z==p3->z))) {
  352.                 fprintf(out, "%striangle", tab);
  353.                 send_XYZ(p1);
  354.                 send_XYZ(p2);
  355.                 send_XYZ(p3);
  356.             }
  357.         }
  358.     }
  359.  
  360. SKIP_REST:
  361.  
  362.     for (obj=object->child; obj; obj=obj->next) {
  363.         if (obj->extr) process_EXTR(obj->extr);
  364.         else process_DESC(obj);
  365.     }
  366.  
  367.     outdent();
  368.     fprintf(out, "%send /* End DESC   \"Object %d at level %d of hierarchy %d\" */\n",
  369.         tab, cur_objnum, num_DESC-num_TOBJ, num_OBJ);
  370.  
  371.     if (desc->posi)
  372.         { fprintf(out, "%stranslate", tab); send_XYZ(desc->posi); }
  373.  
  374.     if (desc->colr) { fprintf(out, "%sambient", tab); send_RGB(desc->colr); }
  375.  
  376.     if (desc->refl) { fprintf(out, "%sspecular", tab); send_RGB(desc->refl); }
  377.  
  378.     if (desc->tran) { fprintf(out, "%s/* TRAN", tab); send_RGB(desc->tran);
  379.         fprintf(out, "*/\n");
  380.     }
  381.  
  382. #ifdef WRITETTDDD
  383.     if (desc->fcount) {
  384.         fprintf(out, "%sCLST Count %u\n", tab, i);
  385.         for (i=0; i<desc->fcount; i++) {
  386.             if (desc->colr) {
  387.                 if (desc->clst[i*3  ]==desc->colr->r &&
  388.                     desc->clst[i*3+1]==desc->colr->g &&
  389.                     desc->clst[i*3+2]==desc->colr->b) continue; /* Skip */
  390.             } else {
  391.                 if (desc->clst[i*3  ]==255 &&
  392.                     desc->clst[i*3+1]==255 &&
  393.                     desc->clst[i*3+2]==255) continue;    /* Skip this one */
  394.             }
  395.             fprintf(out, "%sCLST Color[%u]", tab, i);
  396.             send_RGB((RGB_st*)&desc->clst[i*3]);
  397.         }
  398.         fprintf(out, "%sRLST Count %u\n", tab, i);
  399.         for (i=0; i<desc->fcount; i++) {
  400.             if (desc->refl) {
  401.                 if (desc->rlst[i*3  ]==desc->refl->r &&
  402.                     desc->rlst[i*3+1]==desc->refl->g &&
  403.                     desc->rlst[i*3+2]==desc->refl->b) continue; /* Skip */
  404.             } else {
  405.                 if (desc->rlst[i*3  ]==0 &&
  406.                     desc->rlst[i*3+1]==0 &&
  407.                     desc->rlst[i*3+2]==0) continue;    /* Skip this one */
  408.             }
  409.             fprintf(out, "%sRLST Color[%u]", tab, i);
  410.             send_RGB((RGB_st*)&desc->rlst[i*3]);
  411.         }
  412.         fprintf(out, "%sTLST Count %u\n", tab, i);
  413.         for (i=0; i<desc->fcount; i++) {
  414.             if (desc->tran) {
  415.                 if (desc->tlst[i*3  ]==desc->tran->r &&
  416.                     desc->tlst[i*3+1]==desc->tran->g &&
  417.                     desc->tlst[i*3+2]==desc->tran->b) continue; /* Skip */
  418.             } else {
  419.                 if (desc->tlst[i*3  ]==0 &&
  420.                     desc->tlst[i*3+1]==0 &&
  421.                     desc->tlst[i*3+2]==0) continue;    /* Skip this one */
  422.             }
  423.             fprintf(out, "%sTLST Color[%u]", tab, i);
  424.             send_RGB((RGB_st*)&desc->tlst[i*3]);
  425.         }
  426.     }
  427. #endif
  428.  
  429.     if (desc->tpar) {
  430.         for (i=0; i<16; i++)
  431.             fprintf(out, "%s/* TPAR[%u]=%.12g */\n", tab, i, desc->tpar[i]);
  432.     }
  433.  
  434.     if (desc->surf) {
  435.         for (i=0; i<5; i++)
  436.             fprintf(out, "%s/* SURF[%u]=%d */\n", tab, i, desc->surf[i]);
  437.     }
  438.  
  439.     if (desc->mttr) {
  440.         fprintf(out, "%s/* MTTR Type =%u */\n", tab, desc->mttr->type);
  441.         fprintf(out, "%s/* MTTR Index=%.12g */\n", tab, (double)desc->mttr->indx);
  442.     }
  443.  
  444.     if (desc->spec) {
  445.         fprintf(out, "%s/* SPEC Spec=%u */\n", tab, desc->spec[0]);
  446.         fprintf(out, "%s/* SPEC Hard=%u */\n", tab, desc->spec[1]);
  447.     }
  448.  
  449.     if (desc->prp0) {
  450.         for (i=0; i<6; i++)
  451.             fprintf(out, "%s/* PRP0[%u]=%u */\n", tab, i, desc->prp0[i]);
  452.     }
  453.  
  454.     if (desc->ints)
  455.         fprintf(out, "%s/* INTS=%.12g */\n", tab, desc->ints);
  456.  
  457. #ifdef WRITETDDD
  458.     if (desc->stry) {
  459.         fprintf(out, "%sSTRY Path \"%s\"\n", tab, desc->stry->path);
  460.         fprintf(out, "%sSTRY Translate", tab); send_XYZ(&desc->stry->tran);
  461.         fprintf(out, "%sSTRY Rotate   ", tab); send_XYZ(&desc->stry->rota);
  462.         fprintf(out, "%sSTRY Scale    ", tab); send_XYZ(&desc->stry->scal);
  463.         i = desc->stry->info;
  464.         strin[0] = '\0';
  465.         if (i&(1<<0))  strcat(strin, " ABS_TRA");
  466.         if (i&(1<<1))  strcat(strin, " ABS_ROT");
  467.         if (i&(1<<2))  strcat(strin, " ABS_SCL");
  468.         if (i&(1<<4))  strcat(strin, " LOC_TRA");
  469.         if (i&(1<<5))  strcat(strin, " LOC_ROT");
  470.         if (i&(1<<6))  strcat(strin, " LOC_SCL");
  471.         if (i&(1<<8))  strcat(strin, " X_ALIGN");
  472.         if (i&(1<<9))  strcat(strin, " Y_ALIGN");
  473.         if (i&(1<<10)) strcat(strin, " Z_ALIGN");
  474.         if (i&(1<<12)) strcat(strin, " FOLLOW_ME");
  475.         fprintf(out, "%sSTRY Info%s\n", tab, strin);
  476.     }
  477. #endif
  478.  
  479. SKIP_REST_LIGHT:
  480.  
  481.     cur_objnum++;
  482.     prntline = 0;
  483.     process_TOBJ();
  484. }
  485.  
  486.